package com.itemdoc.organ.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.itemdoc.organ.util.AESUtil;
import com.itemdoc.organ.util.OssUtil;
import com.itemdoc.organ.util.Result;

/**
 * @data weimeilayer@gmail.com
 * @author Administrator
 */
@Controller
@RequestMapping("/file")
public class FileController {
	private Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * 文件上传
	 * http://localhost/upload
	 * @param file
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public Result getfile(MultipartFile file, HttpSession session,ModelMap map) throws Exception{
		try {
			if(file.getInputStream()==null) {
				return new Result(2001, "文件不存在，或者过大！");
			}
			String filename = file.getOriginalFilename();
			if (!"".equals(filename.trim())) {
				File newFile = new File(filename);
				FileOutputStream os = new FileOutputStream(newFile);
				os.write(file.getBytes());
				os.close();
				file.transferTo(newFile);
				// 上传到OSS
				//String uploadUrl = OssUtil.upload(newFile);
				String uploadUrl = OssUtil.uploadSuffix(newFile);
				map.addAttribute("文件下载保存链接：",uploadUrl);
				map.addAttribute("直接访问链接路径",OssUtil.basePath(uploadUrl));
				System.out.println("文件下载保存链接：" + uploadUrl);
				System.out.println("直接访问链接路径：" + OssUtil.basePath(uploadUrl));
				return new Result(200, "上传成功", map);
			}
		} catch (IOException e) {
			log.error("文件不存在，或者过大！");
			System.out.println("文件不存在，或者过大！");
		}
		return new Result(2001, "文件不存在，或者过大！");
	}
	/**
	 * 大文件上传
	 * http://localhost/upload
	 * @param file
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadVoide", method = RequestMethod.POST)
	public Result getfileuploadVoide(MultipartFile file, HttpSession session,ModelMap map) throws Exception{
		try {
			if(file.getInputStream()==null) {
				return new Result(2001, "文件不存在，或者过大！");
			}
			String filename = file.getOriginalFilename();
			if (!"".equals(filename.trim())) {
				File newFile = new File(filename);
				FileOutputStream os = new FileOutputStream(newFile);
				os.write(file.getBytes());
				os.close();
				file.transferTo(newFile);
				// 上传到OSS
				OssUtil.UploadFragmentation(filename);
				System.out.println("视频下载路径："+OssUtil.basePath(filename));
				return new Result(200, "上传成功", OssUtil.basePath(filename));
			}
		} catch (IOException e) {
			log.error("文件不存在，或者过大！");
			System.out.println("文件不存在，或者过大！");
		}
		return new Result(2001, "文件不存在，或者过大！");
	}
	/**
	 * @param 文件下载 
	 * http://localhost/file/downloadFile?objectName=1&localFileName=
	 * @param objectName
	 * @param localFileName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/download", method = RequestMethod.POST)
	public Result downloadFile(String objectName) throws Exception {
		try {
			if ("" != objectName) {
				File file = new File(objectName);
				String fileName = file.getName();
				String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
				String localFileName = "C:\\Users\\Administrator\\Downloads\\" + AESUtil.generateCode(8) + "." + suffix;
				OssUtil.downloadFile(objectName, localFileName);
				System.out.println("下载成功!");
				return new Result(200, "下载成功", localFileName);
			} 
		} catch (Exception e) {
			log.error("下载链接错误，或者文件为空！");
			System.out.println("下载链接错误，或者文件为空！");
		}
		return new Result(2001, "下载链接错误，或者文件为空！");
	}
	/**
	 * @param 删除文件
	 * http://localhost/file/deleteFile?ObjectKey=
	 * @param ObjectKey
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String deleteFile(String ObjectKey) throws Exception{
		OssUtil.deleteFile(ObjectKey);
		return "删除成功";
	}
	 /**
	  * @param  图片处理
	  * @param objectName
	  * @return
	  */
    @ResponseBody
    @RequestMapping(value = "image", method = RequestMethod.GET)
    public String image(String objectName) throws Exception{
    	OssUtil.image(objectName);
        return "success";
    }
}
